
import requests
import json
import base64


class FaceMatch():
    '''\
    

    '''
    
    headers = {'Content-Type': 'application/json; charset=UTF-8'}  #要以此为请求头headers

    def __init__(self,match_url,img_path_1,img_path_2):

        self.match_url = match_url
        self.img_path_1 = img_path_1
        self.img_path_2 = img_path_2
        self.result = self.result()

    def base64_image(self,img_path):
		
        with open(img_path,'rb') as f:
            base64_data = base64.b64encode(f.read())

        return str(base64_data,'utf-8')
		
    def result(self):
		
        params = [
            {
              "image":self.base64_image(self.img_path_1),
              "image_type": "BASE64",
              "face_type": "LIVE",
              "quality_control": "LOW",
            },
            {
              "image":self.base64_image(self.img_path_2),
              "image_type": "BASE64",
              "face_type": "LIVE",
              "quality_control": "LOW",
            },
                ]
        data = json.dumps(params)
        response = requests.post(self.match_url,
                                 data = data,
                                 headers = self.headers)
        result = response.json()
        return result

if __name__ == '__main__':
    from bd_ai_token import BdFaceDetectionGetAccessToken as BFBGA
    match_url = BFBGA().face_match_url()
    #match_url ='https://aip.baidubce.com/rest/2.0/face/v3/match?access_token=24.3c2a9906ad070c63a29809b16dc4c0d3.2592000.1534688195.282335-11564062'
    img_path_1 = 'C:/Users/Administrator/Desktop/\
新建文件夹/img/20180720181429.jpg'
    img_path_2 = 'C:/Users/Administrator/Desktop/\
新建文件夹/img/微信图片_20180720211941.jpg'
    img_path_3 = 'C:/Users/Administrator/Desktop/\
新建文件夹/img/微信图片_20180724220915.jpg'
    img_path_4 = 'C:/Users/Administrator/Desktop/\
新建文件夹/img/微信图片_20180724221115.jpg'
    img_path_5 = 'C:/Users/Administrator/Desktop/\
新建文件夹/img/微信图片_20180724221257.jpg'
    img_path_6 = 'C:/Users/Administrator/Desktop/\
新建文件夹/img/微信图片_20180724221758.jpg'
    img_path_7 = 'C:/Users/Administrator/Desktop/\
新建文件夹/img/微信图片_20180724223408.jpg'
    img_path_8 = 'C:/Users/Administrator/Desktop/\
新建文件夹/img/微信图片_20180724223537.jpg'
    x = FaceMatch(match_url,img_path_2,img_path_8).result
    print(x)

